var t = getApp();

Page({
    data: {
        language: "",
        myvideo: "",
        goods_id: "",
        title: "",
        videoUrl: "",
        isVideo: !0,
        realVideo: "",
        coverUrl: "",
        duration: 0,
        chooseGoods: null,
        videoPath: "",
        videoCover: "",
        limit: 60,
        uploadIng: !1,
        videoProgress: 0,
        showUploadBtn: !1,
        imguploadIng: !1,
        isNeedGoods: !0,
        circleId: 0
    },
    onLoad: function(t) {
        this.data.circleId = t.circleId, this.data.myvideo = wx.createVideoContext("myvideo"), 
        this.init(), this.getList();
        try {
            var i = wx.getStorageSync("shareVideoPath");
            if (i) {
                var e = JSON.parse(i);
                e.title && this.setData({
                    title: e.title
                }), e.videoUrl && this.setData({
                    videoUrl: e.videoUrl
                }), e.videoUrl && this.setData({
                    videoUrl: e.videoUrl,
                    videoPath: e.videoUrl
                }), e.videoCover && this.setData({
                    videoCover: e.videoCover
                });
            }
        } catch (t) {}
    },
    setGoodsId: function(t) {
        t && (this.setData({
            goods_id: t
        }), this.getGoodsById());
    },
    onReady: function() {
        var t = wx.getStorageSync("langIndex");
        this.setData({
            language: t.en
        });
    },
    onShow: function() {},
    onHide: function() {},
    onUnload: function() {},
    onPullDownRefresh: function() {},
    onReachBottom: function() {},
    onShareTimeline: function() {},
    onShareAppMessage: function() {},
    init: function() {
        this.setData({
            title: "",
            videoUrl: "",
            isVideo: !0,
            duration: 0,
            chooseGoods: null,
            videoCover: "",
            limit: "",
            uploadIng: !1,
            showUploadBtn: !1,
            coverUrl: "",
            realVideo: "",
            videoPath: "",
            isNeedGoods: !0
        });
    },
    getList: function() {
        var i = this, e = t.getNetAddresss("plugin.circle.frontend.circle.get-set");
        t._getNetWork({
            url: e,
            data: {},
            success: function(e) {
                var o = e.data;
                1 == o.result ? (i.setData({
                    isNeedGoods: 1 == o.data.select_goods
                }), t._isTextEmpty(o.data.limit) || i.setData({
                    limit: o.data.limit
                })) : t.tips(o.msg);
            },
            fail: function(t) {
                console.log(t);
            }
        });
    },
    getPermission: function() {
        var i = t.getNetAddresss("plugin.video-share.frontend.permission.verify");
        t._getNetWork({
            url: i,
            data: {},
            success: function(t) {
                var i = t.data;
                1 == i.result ? i.data.ret || wx.navigateBack({
                    delta: 1,
                    success: function() {
                        setTimeout(function() {
                            wx.showToast({
                                title: i.msg,
                                icon: "none",
                                duration: 1500
                            });
                        }, 1e3);
                    }
                }) : wx.showToast({
                    title: i.msg,
                    icon: "none",
                    duration: 1500
                });
            },
            fail: function(t) {
                console.log(t);
            }
        });
    },
    getGoodsById: function() {
        var i = this, e = t.getNetAddresss("goods.goods.get-goods-page");
        t._getNetWork({
            url: e,
            data: {
                id: Number(this.data.goods_id)
            },
            success: function(t) {
                var e = t.data;
                1 == e.result ? i.setData({
                    chooseGoods: e.data.get_goods
                }) : wx.showToast({
                    title: e.msg,
                    icon: "none",
                    duration: 1500
                });
            },
            fail: function(t) {
                console.log(t);
            }
        });
    },
    beforeUpVideo: function(t) {
        console.log("开始上传前", t), this.data.realVideo = t.detail, this.setData({
            uploadIng: !0
        });
    },
    successUpVideo: function(i) {
        if (console.log("上传成功后", i), 1 == i.result) {
            this.setData({
                uploadIng: !1,
                videoPath: i.data.img_url,
                videoUrl: i.data.img_url
            });
            var e = {
                title: this.data.title ? this.data.title : "",
                videoUrl: this.data.videoPath ? this.data.videoPath : "",
                videoCover: this.data.videoCover ? this.data.videoCover : ""
            };
            wx.setStorageSync("shareVideoPath", JSON.stringify(e));
        } else this.setData({
            videoUrl: null,
            isVideo: !0,
            duration: 0,
            videoCover: "",
            uploadIng: !1,
            showUploadBtn: !1
        }), t.tips(i.msg);
    },
    deleteImg: function(t) {
        console.log(t), this.data.videoCover = "";
        var i = {
            title: this.data.title ? this.data.title : "",
            videoUrl: this.data.videoPath ? this.data.videoPath : "",
            videoCover: this.data.videoCover ? this.data.videoCover : ""
        };
        wx.setStorageSync("shareVideoPath", JSON.stringify(i));
    },
    beforeUpImg: function() {
        this.setData({
            imguploadIng: !0
        });
    },
    successUpImg: function(i) {
        console.log(i.detail.res);
        var e = i.detail.res;
        if (1 == e.result) {
            this.setData({
                videoCover: e.data.img_url
            });
            var o = {
                title: this.data.title ? this.data.title : "",
                videoUrl: this.data.videoPath ? this.data.videoPath : "",
                videoCover: this.data.videoCover ? this.data.videoCover : ""
            };
            wx.setStorageSync("shareVideoPath", JSON.stringify(o)), this.setData({
                imguploadIng: !1
            });
        } else this.setData({
            imguploadIng: !1
        }), t.tips(e.msg);
    },
    BtnMethos: function() {
        if (this.data.isNeedGoods && t._isTextEmpty(this.data.chooseGoods)) {
            var i = {
                title: this.data.title ? this.data.title : "",
                videoUrl: this.data.videoPath ? this.data.videoPath : "",
                videoCover: this.data.videoCover ? this.data.videoCover : ""
            };
            wx.setStorageSync("shareVideoPath", JSON.stringify(i)), wx.navigateTo({
                url: "/packageH/circleCommunity/circleVideogood/circleVideogood"
            });
        } else {
            if (t._isTextEmpty(this.data.title)) return void wx.showToast({
                title: "请输入4-18个字标题",
                icon: "none",
                duration: 1500
            });
            if (t._isTextEmpty(this.data.videoUrl)) return void wx.showToast({
                title: "请选择视频进行上传！",
                icon: "none",
                duration: 1500
            });
            if (t._isTextEmpty(this.data.videoCover)) return void wx.showToast({
                title: "请选择一张图片作为视频封面图！",
                icon: "none",
                duration: 1500
            });
            var e = {
                goods_id: this.data.goods_id,
                title: this.data.title,
                content: this.data.videoPath,
                img: this.data.videoCover,
                circle_id: this.data.circleId,
                invitation_status: 2,
                address: "",
                store_id: "",
                city: "",
                latitude: "",
                longitude: "",
                ylat: "",
                ylng: ""
            }, o = t.getNetAddresss("plugin.circle.frontend.circle-invitation.release");
            t._postNetWork({
                url: o,
                data: e,
                success: function(t) {
                    var i = t.data;
                    if (1 == i.result) {
                        wx.showToast({
                            icon: "none",
                            title: i.msg,
                            duration: 1500
                        });
                        try {
                            wx.removeStorageSync("shareVideoPath");
                        } catch (t) {}
                        wx.navigateBack({
                            delta: 2
                        });
                    } else wx.showToast({
                        icon: "none",
                        title: i.msg,
                        duration: 1500
                    });
                },
                fail: function(t) {
                    console.log(t.msg);
                }
            });
        }
    },
    titleBlur: function(t) {
        var i = t.detail.value;
        i.length < 4 ? wx.showToast({
            icon: "none",
            title: "请输入不小于4个字",
            duration: 1500
        }) : this.setData({
            title: i
        });
    },
    gotoAdd: function() {
        wx.navigateTo({
            url: "/packageH/circleCommunity/circleVideogood/circleVideogood"
        });
    },
    onReadVideo: function() {
        var t = this;
        wx.chooseVideo({
            sourceType: [ "album", "camera" ],
            maxDuration: 60,
            camera: "back",
            success: function(i) {
                t.getDuration(i.duration, i.tempFilePath);
            }
        });
    },
    getDuration: function(i, e) {
        var o = i;
        return this.data.duration = o, console.log("视频时长：", this.data.duration, this.data.limit + 1, this.data.limit && this.data.duration > this.data.limit + 1), 
        this.data.duration < 1 ? (this.setData({
            videoUrl: null
        }), t.tips("视频时长太短了！")) : 0 != this.data.limit && this.data.duration > parseInt(this.data.limit) + 1 ? (this.setData({
            videoUrl: null
        }), t.tips("视频时长不能超过".concat(this.data.limit, "秒"))) : (this.setData({
            videoUrl: e
        }), void this.setSrcAndCaptureImage(e));
    },
    setSrcAndCaptureImage: function(t) {
        this.beforeUpVideo(t), this.uploadAudioM(t);
    },
    uploadAudioM: function(i) {
        var e = this, o = t.getNetAddresss("upload.uploadPic");
        o += "&upload_type=video", wx.uploadFile({
            url: o,
            filePath: i,
            name: "file",
            formData: {},
            success: function(t) {
                var i = JSON.parse(t.data);
                1 != i.result && e.setData({
                    videoUrl: null
                }), e.successUpVideo(i);
            }
        });
    },
    clearVideo: function() {
        this.setData({
            videoUrl: null
        }), wx.getStorageSync("shareVideoPath") && wx.clearStorageSync("shareVideoPath");
    }
});